package com.lx.controller;

import com.lx.bean.*;
import com.lx.mvc.ResponseBody;
import com.lx.service.CourierService;
import com.lx.service.UserService;
import com.lx.util.DateFormatUtil;
import com.lx.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CourierController
 * @Description:
 * @Date Created in  2021-07-09 20:46
 * @Modified By:
 * @Author: 零巽
 */
public class CourierController {

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        //获取前端信息
        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        //封装为对象
        Courier courier = new Courier(userName, phone, idNumber, password);
        //调用Service方法
        boolean b = CourierService.addCourier(courier);
        //根据结果封装信息
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("添加成功！");
            User byPhone = UserService.findByPhone(phone);
            if (byPhone == null) {
                User user = new User(userName, phone, idNumber, password);
                UserService.addUser(user);
            }
        } else {
            message.setStatus(-1);
            message.setResult("电话号码已存在，添加失败！");
        }
        //封装为Json对象返回
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/courier/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp){
        String inputPhone = req.getParameter("inputPhone");
        Courier courier = CourierService.findByPhone(inputPhone);
        Message message = new Message();
        if (courier != null) {
            message.setStatus(0);
            message.setData(courier);
        } else {
            message.setStatus(-1);
            message.setResult("查询失败，手机号码不存在！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String inputPhone = req.getParameter("inputPhone");
        boolean b = CourierService.deleteByPhone(inputPhone);
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("删除成功!");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String idNumber = req.getParameter("idNumber");
        String password = req.getParameter("password");
        Courier courier = new Courier(userName, phone, idNumber, password);
        boolean update = CourierService.update(Integer.parseInt(id), courier);
        Message message = new Message();
        if (update) {
            message.setStatus(0);
            message.setResult("更新成功！");
            User byPhone = UserService.findByPhone(phone);
            if (byPhone == null) {
                User user = new User(userName, phone, idNumber, password);
                UserService.addUser(user);
            }
        } else {
            message.setStatus(-1);
            message.setResult("电话号码已存在，更新失败！");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/courier/list.do")
    public String getCourierList(HttpServletRequest req, HttpServletResponse resp){
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        List<Courier> allCourier = CourierService.getAllCourier(true, Integer.parseInt(offset), Integer.parseInt(pageNumber));
        List<BootstrapTableCourier> couriers = new ArrayList<>();
        for (Courier courier : allCourier) {
            String idNumber = courier.getIdNumber();
            String password = courier.getPassword();
            String phone = courier.getPhone();
            String userName = courier.getUserName();
            String loginTime =null;
            String registerTime = null;
            int id = courier.getId();
            if (courier.getLoginTime() != null) {
                loginTime = DateFormatUtil.format(courier.getLoginTime());
            }
            if (courier.getRegisterTime() != null) {
                registerTime = DateFormatUtil.format(courier.getRegisterTime());
            }
            int sendNumber = courier.getSendNumber();
            BootstrapTableCourier bootstrapTableCourier = new BootstrapTableCourier(id, userName, phone, idNumber, password, sendNumber, registerTime, loginTime);
            couriers.add(bootstrapTableCourier);
        }
        ResultData<BootstrapTableCourier> resultData = new ResultData<>();
        resultData.setRows(couriers);
        int[] console = CourierService.console();
        resultData.setTotal(console[0]);
        return JsonUtil.toJSON(resultData);
    }

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        int[] console = CourierService.console();
        Message message = new Message();
        if (console != null) {
            message.setStatus(0);
            message.setData(console);
        } else {
            message.setStatus(-1);
        }
        return JsonUtil.toJSON(message);

    }

}
